package ftest.movie.pages;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;

import ftest.movie.model.Film;

public class MainPage extends AnyPage {

	public MainPage(PageManager pages) {
		super(pages);
	}
	
	@FindBy(xpath = "//a[@href = './?go=add']")
    private WebElement addButton;
	
	@FindBy(xpath = "//a[@href = 'http://localhost/php4dvd/']")
    private WebElement mailPageLogo;
	
	@FindBy(xpath = "//*[@class = 'content' and text() = 'No movies where found.']")
	public WebElement isNoMoviesFoundMsg;
	
	@FindBy(id = "q")
	public WebElement searchField;
	
	@FindBy(className = "ui-selectmenu-status")
	public WebElement sortSelect;
	
	@FindBy(id = "results")
	public WebElement filmsTable;
	
	@FindBy(id = "sort-button")
	public WebElement sortButton;
	
	@FindBy(xpath = "//span[contains(text(), 'Sort by name (A-Z)')]")
	public WebElement sortAZ;
	
	@FindBy(xpath = "//span[contains(text(), 'Sort by name (Z-A)')]")
	public WebElement sortZA;
	
	public WebElement movieInMainPage(String name){
		return driver.findElement(By.xpath(String.format("//div[@id = 'results']//div[@title = '%s']", name)));
	}
	
	public WebElement movieByNumber(int num){
		return driver.findElement(By.xpath(String.format("(id('results')//div[@class = 'title'])['%s']", num)));
	}
	
	public void clickAddButton(){
		addButton.click();
	}
	
	public void open(){
		mailPageLogo.click();
	}

	public void openFilmByName(String movieName) {
		clickMovieInMainPage(movieName);		
	}

	public void clickMovieInMainPage(String movieName) {
		movieInMainPage(movieName).click();		
	}

	public void clickSortButton() {
		sortButton.click();		
	}

	public void selectSortAZ() {
		sortAZ.click();		
	}
	
	public void selectSortZA() {
		sortZA.click();		
	}

	public String getFirstFilmFromTable() {
		return getFilmFromTable(1);
		
	}
	
	public String getSecondFilmFromTable() {
		return getFilmFromTable(2);
		
	}
	
	public String getThirdFilmFromTable() {
		return getFilmFromTable(3);
		
	}
	
	public String getFilmFromTable(int num) {
		return movieByNumber(num).getText();		
	}
}
